package com.todod.service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.todod.common.util.SnowflakeIdGenerator;
import com.todod.dto.ShuoMingCatalogDto;
import com.todod.mapper.ShuoMingCatalogMapper;
import com.todod.model.ShuoMingCatalog;
import com.todod.model.ShuoMingFile;

@Service
@Transactional
public class ShuoMingCatalogService {

	@Autowired
	private ShuoMingCatalogMapper shuoMingCatalogMapper;
	@Autowired
	private ShuoMingFileService shuoMingFileService;

	/**
	 * 
	 * @param _userId
	 * @param shuoMingCatalog
	 * @param value
	 * @param value2
	 */

	public void saveData(Long _userId, ShuoMingCatalog shuoMingCatalog) {

		shuoMingCatalog.setId(SnowflakeIdGenerator.getId());

		shuoMingCatalog.setCreatetimeTi(LocalDateTime.now());
		shuoMingCatalog.setCreateuserId(_userId);
		shuoMingCatalogMapper.insert(shuoMingCatalog);
	}

	/**
	 * 
	 * @param _userId
	 * @param shuoMingCatalog
	 * @param value
	 * @param value2
	 */
	public void editData(Long _userId, ShuoMingCatalog shuoMingCatalog) {

		ShuoMingCatalog shuoMingCatalog1 = shuoMingCatalogMapper.selectById(shuoMingCatalog.getId());

		shuoMingCatalog.setCreatetimeTi(shuoMingCatalog1.getCreatetimeTi());
		shuoMingCatalog.setCreateuserId(shuoMingCatalog1.getCreateuserId());
		shuoMingCatalog.setModifytimeTi(LocalDateTime.now());
		shuoMingCatalog.setModifyuserId(_userId);
		shuoMingCatalogMapper.updateById(shuoMingCatalog);
	}

	/**
	 * 
	 * @param code
	 * @return
	 */
	public Map<String, Object> getInfo(Long code) {
		Map<String, Object> map = new HashMap<>();
		ShuoMingCatalogDto shuoMingCatalog = shuoMingCatalogMapper.getInfo(code);

		map.put("shuoMingCatalog", shuoMingCatalog);
		return map;
	}

	/**
	 * 
	 * @param _userId
	 * @param string
	 */
	public void complete(Long _userId, String id) {
		ShuoMingCatalog shuoMingCatalog = shuoMingCatalogMapper.selectById(Long.valueOf(id));

		shuoMingCatalog.setModifytimeTi(LocalDateTime.now());
		shuoMingCatalog.setModifyuserId(_userId);
		shuoMingCatalogMapper.updateById(shuoMingCatalog);
	}

	/**
	 * 
	 * @param xiangmuId
	 * @return
	 */
	public List<ShuoMingCatalog> getListByParentId(Long parentId) {

		QueryWrapper<ShuoMingCatalog> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("dm_parent_id", parentId);
		queryWrapper.orderByAsc("dm_sort_num");

		List<ShuoMingCatalog> list = shuoMingCatalogMapper.selectList(queryWrapper);

		return list;
	}

	/**
	 * 
	 * @param xiangmuId
	 * @return
	 */
	public List<ShuoMingCatalog> getListByXmId(Long xiangmuId) {

		QueryWrapper<ShuoMingCatalog> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("dm_xm_id", xiangmuId);
		queryWrapper.orderByAsc("dm_sort_num");

		List<ShuoMingCatalog> list = shuoMingCatalogMapper.selectList(queryWrapper);

		return list;
	}

	/**
	 * 
	 * @param xiangmuId
	 * @param parentId
	 * @return
	 */
	public List<ShuoMingCatalog> getListByXmIdAndParentId(Long xiangmuId, Long parentId) {

		QueryWrapper<ShuoMingCatalog> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("dm_xm_id", xiangmuId);
		queryWrapper.eq("dm_parent_id", parentId);
		queryWrapper.orderByAsc("dm_sort_num");

		List<ShuoMingCatalog> list = shuoMingCatalogMapper.selectList(queryWrapper);

		return list;
	}

	/**
	 * 
	 * @param _userId
	 * @param xmId
	 * @return
	 */
	public List<ShuoMingCatalog> getListByUserIdAndType(Long _userId, Long xmId) {
		List<ShuoMingCatalog> returnList = new ArrayList<ShuoMingCatalog>();

		ShuoMingCatalog shuoMingCatalog = new ShuoMingCatalog();
		shuoMingCatalog.setId(xmId);
		shuoMingCatalog.setParentId(-1L);
		shuoMingCatalog.setSortNum(0);
		shuoMingCatalog.setName("api目录");
		shuoMingCatalog.setFlag("ML");
		shuoMingCatalog.setXmId(xmId);

		returnList.add(shuoMingCatalog);

		QueryWrapper<ShuoMingCatalog> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("dm_xm_id", xmId);
		queryWrapper.eq("dm_parent_id", xmId);
		queryWrapper.orderByAsc("dm_sort_num");

		List<ShuoMingCatalog> list = shuoMingCatalogMapper.selectList(queryWrapper);

		for (ShuoMingCatalog shuoMingCatalog1 : list) {
			shuoMingCatalog1.setFlag("ML");
			returnList.add(shuoMingCatalog1);
		}

		return returnList;
	}

	/**
	 * 
	 * @param _userId
	 * @param string
	 * @param departid
	 * @param parentId
	 * @return
	 */
	public List<ShuoMingCatalog> getListByUserIdAndTypeAndParentId(Long _userId, Long xmId, Long parentId) {
		List<ShuoMingCatalog> returnList = new ArrayList<ShuoMingCatalog>();

		QueryWrapper<ShuoMingCatalog> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("dm_xm_id", xmId);
		queryWrapper.eq("dm_parent_id", parentId);
		queryWrapper.orderByAsc("dm_sort_num");

		List<ShuoMingCatalog> list = shuoMingCatalogMapper.selectList(queryWrapper);
		if (list != null && list.size() > 0) {
			for (ShuoMingCatalog shuoMingCatalog : list) {
				shuoMingCatalog.setFlag("ML");
				returnList.add(shuoMingCatalog);
			}
		} else {// 获取个人创建数据
			List<ShuoMingFile> plist = shuoMingFileService.getListByClassifyId(parentId);
			for (ShuoMingFile shuoMingFile : plist) {
				ShuoMingCatalog shuoMingCatalog = new ShuoMingCatalog();
				shuoMingCatalog.setId(shuoMingFile.getId());
				shuoMingCatalog.setParentId(parentId);
				shuoMingCatalog.setSortNum(shuoMingFile.getSortNum());
				shuoMingCatalog.setName(shuoMingFile.getTableName());
				shuoMingCatalog.setFlag("WD");
				shuoMingCatalog.setXmId(xmId);

				returnList.add(shuoMingCatalog);
			}
		}

		return returnList;
	}

}
